#include <iostream>
#include "common.h"

using namespace std;

int main()
{
    // 服务端创建共享内存
    key_t key = getKey();
    int shmid = createShm(key, gsize);

    cout << "server key: " << toHEX(key) << endl;
    cout << "server shmid: " << shmid << endl;

    char *start = (char *)shmat(shmid, NULL, 0);
    if ((void *)start == (void *)-1)
    {
        cerr << "shmat fail!"
             << "errno: " << errno << " | " << strerror(errno) << endl;
        shmctl(shmid, IPC_RMID, NULL); // 即使异常了，也要把共享内存释放
        exit(1);
    }

    // 挂接成功后，睡20秒再释放
    printf("start: %p\n", start);
    sleep(20);

    shmctl(shmid, IPC_RMID, NULL);
    return 0;
}
